package com.ahuan.exception;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 第二种拦截器异常处理
 */
//@Component
public class InterceptorException2 implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        // 在这里编写异常处理逻辑
        try {
            response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "An error occurred: " + ex.getMessage());
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        // 返回null表示已经处理了异常，不需要再交给其他的异常解析器处理
        return new ModelAndView();
    }
}
